package com.gy.boot.common.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Random;

/**
 * HY 互亿无线 短信发送
 * */
public class SendSmsUtil {

    private static Logger LOGGER = LoggerFactory.getLogger(SendSmsUtil.class);

    /**
     * 获取随机6位数字验证码
     * */
    public static String getRandomCode(){
        return ""+(new Random().nextInt(899999)+100000);
    }

    private static final String API_ID = "C97870532";
    private static final String API_KEY = "910a448e63a2c1df8002215feac95e8b";
    private static final String URL = "http://106.ihuyi.com/webservice/sms.php?method=Submit";

    /**
     * 发送短信验证码
     * */
    public static boolean sendSmsCode(String phone, String code) {
        String content = "您的验证码是：" + code + "。请不要把验证码泄露给其他人。";
        return sendSMS(phone, content);
    }

    /**
     * 发送短信验证码
     * */
    public static boolean sendSmsCode2(String phone) {
        String content =  "尊敬的测试您好，您所在测试公司为您注册成为测试会员，您可通过微信搜索测试公众号进行签约认证。";
//        String content =  "您好，测试校验码:666,验证码有效期为3分钟，请勿泄露";
        return sendSMS(phone, content);
    }

    /**
     * 发送短信
     * */
    public static boolean sendSMS(String phone, String content) {
        LOGGER.info("======= 短信发送开始 ========");
        Map<String, Object> params = MapUtil.newHashMap(4);
        params.put("account", API_ID);
        params.put("password", API_KEY);
        params.put("mobile", phone);
        params.put("content", content);
        System.out.println("发送内容: ");
        System.out.println(content);
        String result = HttpUtil.post(URL, params);
        System.out.println(result);
        boolean isSendSmsSuccess = false;
        LOGGER.info("======= 短信发送结束 ========");
        return isSendSmsSuccess;
    }

    public static void main(String[] args) {
        String code = getRandomCode();
//        boolean isSendSuccess = sendSmsCode("18281916257", code);
        boolean isSendSuccess = sendSmsCode2("18281916257");
//        System.out.println("短信发送验证码: " + code);
//        System.out.println("短信发送状态: " + isSendSuccess);
    }

}
